﻿using Newtonsoft.Json;
using Opc.Ua;
using Opc.Ua.Client;
using SHRIS.MIS.Common.Enum.APIExternal;
using SHRIS.MIS.Common.Util.Extension;
using SHRIS.MIS.Entity.ScadaBaseInfo;
using SHRIS.MIS.Entity.ScadaData;
using SHRIS.MIS.Service.LinePlantService;
using SHRIS.MIS.Service.OpcService;
using SHRIS.MIS.Service.RabbitService;
using ShrisCommunicationCore.OpcUa;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SHRIS.MIS.Service.Device
{
    public class LoadStation_01 : BasicDevice
    {


        #region device parameters

        private string _materialCode;
        public string MaterialCode
        {
            get
            {
                return _materialCode;
            }
            set
            {
                string previewVlaue = _materialCode;
                _materialCode = value;
                OnPropertyChanged("MaterialCode", previewVlaue, _materialCode);
            }
        }

        public int MaterialIndex { get; set; }

        public DateTimeOffset LoadingTime { get; set; }

        #endregion

        #region device config

        #endregion

        private readonly IEnumerable<OpcDataSourceEntity> _opcDataSourceEntities;
        public LoadStation_01(DeviceEntity deviceEntity,
            WorkshopCommunicationService workshopCommunicationService,
            IEnumerable<OpcDataSourceEntity> opcDataSourceEntities) 
            : base(deviceEntity, workshopCommunicationService)
        {          
            _opcDataSourceEntities = opcDataSourceEntities;
            CheckOutDataType();
        }

        private void CheckOutDataType()
        {
            //对所有OPC 数据类型进行校验，如果不对记入LOG

        }


        public override void InitalDevice()
        {
            //创建opc 订阅

            //找出该通道下的订阅数据，dataAction==4 为订阅
            var subscriptionDatas = _opcDataSourceEntities.Where(i => i.DataAction == 4);

            //找出订阅数据的服务器列表
            var serverIds = subscriptionDatas.Select(i => i.ServerId).Distinct().ToList();

            if (serverIds.Count() == 1)
            {
                _workshopCommunicationService.CreateOpcSubscription(subscriptionDatas, DataHandler);
            }


            //用deviceCode作为订阅组名字
            //var subscriptionName = _deviceEntity.DeviceCode;

        }

        private void DataHandler(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
                //Console.WriteLine(notification.Value.StatusCode.ToString());

                if (notification != null)
                {
                    Guid dataId = new Guid();
                    bool isId = Guid.TryParse(monitoredItem.DisplayName, out dataId);
                    if (isId && (!dataId.IsGuidNullOrEmpty()))
                    {
                        var dataSource = _opcDataSourceEntities.Where(i => i.Id == dataId).First();
                        switch(dataSource.DataName)
                        {
                            case "MaterialCode":
                                {
                                    MaterialCode = notification.Value.Value.ToString();                                 
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
